package com.example.script.portal.config;

import com.example.script.model.SysUser;
import com.example.script.portal.model.PortalUserDetails;
import com.example.script.portal.service.UserService;
import com.example.script.security.config.SecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@EnableWebSecurity
@Configuration
public class PortalSecurityConfig extends SecurityConfig {
    @Autowired
    private UserService userService;

    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        return new UserDetailsService() {//实现了个匿名内部类（直接实现）
            @Override
            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
                SysUser sysUser = userService.getUserByUsername(username);
                if (sysUser !=null){
                    return new PortalUserDetails(sysUser);
                }
                return null;
            }
        };
    }
}
